<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-button type="primary" size="mini" @click="delAll">批量删除</el-button>
        <el-button type="primary" size="mini" @click="centerDialogVisible = true">添加歌单</el-button>
        <el-input v-model="select_word" size="mini" placeholder="请输入歌单名" class="handle-input"></el-input>
      </div>
    </div>

    <!-- 主界面 -->
    <el-table size="mini" ref="multipleTable" border style="width:100%" :data="data" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column label="歌单图片" width="200" align="center">
        <template slot-scope="scope">
          <div class="songList-img">
            <img :src="getUrl(scope.row.pic)" style="width:100%" height=200px>
          </div>
          <el-upload :action="uploadUrl(scope.row.id)" :before-upload="beforeAvatorUpload" 
            :on-success="handleAvatorSuccess">
            <el-button size="mini">更新图片</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题" width="120" align="center"></el-table-column>
      <el-table-column prop="introduction" label="专辑" width="150" align="center"></el-table-column>
      <el-table-column label="简介" align="center">
        <template slot-scope="scope">
          <p style="height:100px;overflow:scroll">{{scope.row.introduction}}</p>
        </template>
      </el-table-column>
      <el-table-column prop="style" label="风格" width="120" align="center"></el-table-column>
      <el-table-column label="歌曲管理" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="songEdit(scope.row.id)">歌曲管理</el-button>
        </template>
      </el-table-column>
      <el-table-column label="评论" width="80" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="getComment(data[scope.$index].id)">评论</el-button>
        </template>    
      </el-table-column>  
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          background
          layout = "total,prev,pager,next"
          :page-size="pageSize"
          :total="tableData.length"
          @current-change="handleCurrentChange"
          >
      </el-pagination>
    </div>

    <!-- 添加歌单界面 -->
    <el-dialog title="添加歌单" :visible.sync="centerDialogVisible" width="400px" center >
      <el-form :model="registerForm" ref="registerForm" label-width="80px" action="" id="tf">
        <el-form-item prop="title" label="标题" size="mini">
          <el-input v-model="registerForm.title" placeholder="标题"></el-input>
        </el-form-item>
        <el-form-item prop="introduction" label="简介" size="mini">
          <el-input v-model="registerForm.introduction" placeholder="简介"></el-input>
        </el-form-item>
        <el-form-item prop="style" label="风格" size="mini">
          <el-input v-model="registerForm.style" placeholder="风格"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" size="mini" @click="addSongList">确认</el-button>
        <el-button size="mini" @click="centerDialogVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <!-- 修改歌单界面 -->
    <el-dialog title="修改歌单" :visible.sync="editVisible" width="400px" center>
      <el-form :model="form" ref="form" label-width="80px">
        <el-form-item prop="id" label="id" size="mini">
          <el-input v-model="form.id" disabled="disabled"></el-input>
        </el-form-item>
        <el-form-item prop="title" label="标题" size="mini">
          <el-input v-model="form.title" placeholder="标题"></el-input>
        </el-form-item>                
        <el-form-item prop="introduction" label="简介" size="mini">
          <el-input v-model="form.introduction" placeholder="简介"></el-input>
        </el-form-item> 
        <el-form-item prop="style" label="风格" size="mini">
          <el-input v-model="form.style" placeholder="风格"></el-input>
        </el-form-item> 
      </el-form>
      <span slot="footer">
        <el-button type="primary" size="mini" @click="editSave">确认</el-button>
        <el-button size="mini" @click="editVisible = false">取消</el-button>
      </span>
    </el-dialog>

    <el-dialog title="删除歌单" :visible.sync="delVisible" width="300px" center>
      <div align="center">删除不可恢复，是否确认删除</div>
      <span slot="footer">
        <el-button type="primary" size="mini" @click="deleteRow">确认</el-button>
        <el-button size="mini" @click="delVisible = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mixin } from '../mixins/index';
import { getAllSongList,setSongList,updateSongList,delSongList } from "@/api/index"
export default {
  mixins: [mixin],
  name: '',
  components: {},
  props: {},
  data () {
    return {
      centerDialogVisible: false, // 添加弹窗是否显示
      editVisible: false,         // 编辑弹窗是否显示
      delVisible: false,          // 删除弹窗是否显示
      registerForm: {             // 添加框
        title: '',
        introduction: '',
        style: ''
      },
      form: {        // 编辑框
        id: '',
        title: '',
        introduction: '',
        style: ''
      },
      tableData: [], // 歌单数据表
      tempData: [], // 根据歌单名搜索时用的歌单数据表
      select_word: '', // 搜索的歌单名
      pageSize: 10, // 分页每页大小
      currentPage: 1, // 当前页
      idx: -1, // 当前选择的歌单
      multipleSelection: [],   //哪些项已经打勾
    }
  },
  computed: {
    //计算当前搜索结果表里的数据
    data(){
        return this.tableData.slice((this.currentPage - 1) * this.pageSize,this.currentPage * this.pageSize)
    }
  },
  watch: {
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function() {
      if(this.select_word == ''){
        this.tableData = this.tempData;
      } else {
        this.tableData = [];
        for(let item of this.tempData) {
          if(item.title.includes(this.select_word)){
            this.tableData.push(item);
          }
        }
      }
    }
  },
  created () {
    this.getData();
  },
  methods: {
    // 获取全部歌单
    getData() {
      this.tableData = [];
      this.tempData = [];
      getAllSongList().then(res =>{
        this.tableData = res;
        this.tempData = res;
        this.currentPage = 1;
      })
    },
    //获取当前页
    handleCurrentChange(val){
        this.currentPage = val;
    },
    //弹出编辑页面
    handleEdit(row) {
      this.editVisible = true;
      this.form = {
        id: row.id,
        title: row.title,
        introduction: row.introduction,
        style: row.style
      }
    },
    // 保存编辑
    editSave() {
      let params = new URLSearchParams();
      params.append('id',this.form.id);
      params.append('title',this.form.title);
      params.append('style',this.form.style);
      params.append('introduction',this.form.introduction);
      usl(params)
      updateSongList.then(res => {
        if(res.code == 1){
          this.getData();
          this.notify("修改成功","success");
        }else{
          this.notify("修改失败","error");
        }
      })
      .catch(err => {
        console.log(err)
      })
      this.editVisible = false;
    },
    //添加歌单
    addSongList(){
      let params = new URLSearchParams();
      params.append('title',this.registerForm.title);
      params.append('pic','/img/songListPic/123.jpg');
      params.append('introduction',this.registerForm.introduction);
      params.append('style',this.registerForm.style);

      setSongList(params)
      .then(res => {
        if(res.code == 1){
          this.getData();
          this.notify("添加成功","success");
        }else{
          this.notify("添加失败","error");
        }
      })
      .catch(err => {
        console.log(err);
      });
      this.centerDialogVisible = false;
    },
    // 更新图片路径
    uploadUrl(id){
      return `${this.$store.state.HOST}/songList/updateSongListPic?id=${id}`;
    },
    // 删除一个歌单
    deleteRow() {
      delSongList(this.idx)
      .then(res => {
        if(res) {
          this.getData();
          this.notify("删除成功","success");
        } else {
          this.notify("删除失败","error");
        }
      })
      .catch(err => {
        console.log(err);
      }) 
      this.delVisible = false;
    },
    // 转向歌曲管理界面
    songEdit(id) {
      this.$router.push({path: `/ListSong`,query:{id}});
    },
    //转向该歌单的评论列表
    getComment(id){
      this.$router.push({path: '/comment',query:{id}})
    }
  }
}
</script>
<style>
.handle-box {
  margin-bottom: 10px;
}
.handle-input{
    width: 300px;
    display: inline-block;
}
</style>